<?php


namespace App\Kuafu\CustomerCoupon\Convert;


use App\Kuafu\Coupon\Service\CouponService;
use App\Kuafu\CustomerCoupon\Enum\CodeEnum;

class CustomerCouponConvent
{
    /**
     * @param $data
     * @return array
     */
    public static function customerCouponListConvent($data): array
    {
        if (count($data['list']) == 0){
            return $data;
        }
        foreach ($data['list'] as $k => $v) {
            $data['list'][$k]['couponImg']           = picturePath($v['couponImg']);
            $data['list'][$k]['statusDesc']          = CodeEnum::COUPON_STATUS_TEXT[$v['status']];
            $data['list'][$k]['couponTypeDesc']      = CodeEnum::COUPON_TYPE_TEXT[$v['couponType']];
            $data['list'][$k]['useTimeTypeDesc']     = CodeEnum::COUPON_USE_TIME_TYPE_TEXT[$v['useTimeType']];
            $data['list'][$k]['couponStatusValue']   = self::getCustomerCouponStatus($v);
            $data['list'][$k]['availableSourceText'] = CouponService::availableSourceRules($v['coupon_rel']['availableSource']);
            $data['list'][$k]['startAtDateTime']     = formatDateYmd($v['startAt']);
            $data['list'][$k]['endAtDateTime']       = formatDateYmd($v['endAt']);
        }
        return $data;
    }

    public static function getCustomerCouponStatus($value)
    {
        $time        = time();
        $statusValue = 1;
        if ($value['endAt'] < $time) {
            $statusValue = CodeEnum::CUSTOMER_COUPON_STATUS_EXPIRE;
        } elseif ($value['startAt'] > $time) {
            $statusValue = CodeEnum::CUSTOMER_COUPON_STATUS_NOT_EFFECTIVE;
        } elseif ($value['startAt'] < $time && $value['endAt'] > $time) {
            $statusValue = CodeEnum::CUSTOMER_COUPON_STATUS_EFFECTIVE;
        }
        if ($value['status'] == CodeEnum::COUPON_STATUS_USED || $value['status'] == CodeEnum::COUPON_STATUS_LOCK) {
            $statusValue = CodeEnum::CUSTOMER_COUPON_STATUS_USED;
        }
        return $statusValue;
    }


    /***
     * 根据$availableSource 获取搜索条件
     *
     * @param $availableSource
     * @return array|int[]
     */
    public static function getAvailableSourceIds($availableSource): array
    {
        // 值为累加字段， 目前 0和3 都表示全部
        switch ($availableSource){
            case CodeEnum::AVAILABLE_SOURCE_ALL:
            case CodeEnum::AVAILABLE_SOURCE_ALL_THREE:
                $availableSourceList = [CodeEnum::AVAILABLE_SOURCE_ALL, CodeEnum::AVAILABLE_SOURCE_ALL_THREE];
                break;
            case CodeEnum::AVAILABLE_SOURCE_ONE:
                $availableSourceList = [CodeEnum::AVAILABLE_SOURCE_ALL, CodeEnum::AVAILABLE_SOURCE_ONE, CodeEnum::AVAILABLE_SOURCE_ALL_THREE];
                break;
            case CodeEnum::AVAILABLE_SOURCE_TWO:
                $availableSourceList = [CodeEnum::AVAILABLE_SOURCE_ALL, CodeEnum::AVAILABLE_SOURCE_TWO, CodeEnum::AVAILABLE_SOURCE_ALL_THREE];
                break;
            default:
                $availableSourceList = [];
                break;
        }
        return $availableSourceList;

    }
}
